<template>
    <div>
        <div class="editor-title">
            <span class="editor-title__name">{{ data.text }}</span>
        </div>

        <div style="padding: 16px">
            <div class="editor-two__item">
                <div class="editor-two__item-head">标题名称</div>
                <el-input v-model="data.data.shop_name" placeholder="输入标题名称" clearable />
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">标题颜色</div>
                <div>
                    <el-color-picker v-model="data.style.head_color" @change="color_change('head_color', '#fff')" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con" style="flex: 1">渐变颜色</div>
                <div>
                    <el-color-picker v-model="data.style.start_color" @change="color_change('start_color', '#ffeeea')" />
                </div>
            </div>
            <div class="cls-between" style="padding-bottom: 1rem">
                <div class="label-con">商家数量</div>
                <div class="cls-between" style="width: 108px">
                    <el-input v-model="data.data.reqData.limit" oninput="value=value.replace(/[^\d]/g,'')"
                        placeholder="输入商品数量" clearable @input="changeLimit" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">页面边距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['margin-left']" :max="max" @change="padding_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model="data.rpxStyle['margin-left']" @input="padding_change" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">圆角距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['border-radius']" :max="30" @change="radius_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['border-radius']" @input="radius_change" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">上边距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['margin-top']" :min="0" :max="300" @change="mtt_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['margin-top']" @input="mtt_change" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 1rem">
                <div class="label-con">下边距</div>
                <div style="flex: 1; padding-right: 1rem">
                    <el-slider v-model="data.rpxStyle['margin-bottom']" :min="0" :max="300" @change="mtb_change" />
                </div>
                <div style="width: 4rem">
                    <el-input v-model.number="data.rpxStyle['margin-bottom']" @input="mtb_change" />
                </div>
            </div>
            <div class="cls-flex" style="padding-bottom: 8px">
                <div class="label-con" style="flex: 1">自定义图标<span class="label-con__span">（建议：30x30像素）</span></div>
                <div>
                    <el-switch v-model="data.style.icon_switch" width="60px" size="large" inline-prompt active-text="开启"
                        inactive-text="关闭" @change="changeSwitch" />
                </div>
            </div>
            <div style="padding-bottom: 1rem" v-if="data.style.icon_switch">
                <div>
                    <div style="position: relative; cursor: pointer" @click="showImg = true">
                        <img :src="data.data.custom_icon" style="width: 60px; height: 60px" v-if="data.data.custom_icon" />
                        <div class="cls-center" style="width: 60px; height: 60px; background-color: #f6f6f6" v-else>
                            <el-icon size="20px" color="#999">
                                <Plus />
                            </el-icon>
                        </div>
                        <div class="tip-con">{{ data.data.custom_icon ? "更换图片" : "添加图片" }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 图片库 -->
    <picture-library :show="showImg" @close="showImg = false" @select="selectImg" :select_multiple="false">
    </picture-library>
</template>

<script setup>
import { getCurrentInstance, ref, onMounted } from "vue"
import { Plus } from "@element-plus/icons-vue"
import PictureLibrary from "@components/PictureLibrary.vue"

const { proxy } = getCurrentInstance()
const max = 16 // 最大值，适用于内外边距和圆角

/**
 * 门店列表-供应商
 * @property {Object} pageData  页面数据
 */
const props = defineProps({
    data: {
        type: Object,
        default: {
            id: 7,
            type_name: "shopList",
            text: "门店列表",
            data: {
                custom_icon: "",
                /**请求地址*/
                reqUrl: "/api/supplier/items",
                /**请求参数*/
                reqData: { limit: 6 },
                /* 当没有请求地址的时候，使用data数据 */
                data: [],
            },
        },
    },
})

onMounted(() => {
    getShop()
})

// 修改商品显示条数
const changeLimit = (val) => {
    getShop()
}
// 获取商家列表
function getShop() {
    proxy.$axios1
        .get(`${props.data.data.reqUrl}?limit=${props.data.data.reqData.limit}`)
        .then((res) => {
            if (res.data.code == 0) {
                let result = res.data.data
                result.map((item) => {
                    item.avg_score = item.avg_score == 0 ? 5 : Number(item.avg_score)
                })
                props.data.data.data = result
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => { })
}

const padding_change = (val) => {
    val = val > max ? max : val
    props.data.rpxStyle["margin-left"] = props.data.rpxStyle["margin-right"] = Number(val)
}

// 圆角变化
const radius_change = (val) => {
    val = val > max ? max : val
    props.data.rpxStyle["border-radius"] = Number(val)
}

// 上下边距
const mtt_change = (val) => {
    props.data.rpxStyle["margin-top"] = Number(val)
}
const mtb_change = (val) => {
    props.data.rpxStyle["margin-bottom"] = Number(val)
}


/**
 * 当点击清空颜色时，恢复默认颜色
 * @param {*} key 要修改的颜色字段
 * @param {*} color 默认颜色， 当key值为null时，恢复默认颜色
 */
const color_change = (key, color) => {
    if (!props.data.style[key]) {
        props.data.style[key] = color
    }
}

// 图标选择
const showImg = ref(false)
const selectImg = (val) => {
    showImg.value = false
    props.data.data.custom_icon = proxy.imgUrl + val[0].photo
}

const changeSwitch = (val) => {
    props.data.data.custom_icon = ""
}
</script>

<style lang="less" scoped>
.editor-two__item {
    margin-bottom: 20px;

    .editor-two__item-head {
        color: #333;
        line-height: 46px;
    }
}

.editor-title {
    padding: 24px 16px;
    border-bottom: 1px solid #f2f4f6;
    display: flex;
}

.editor-title__name {
    font-size: 18px;
    font-weight: 600;
    line-height: 24px;
    color: #333;
}

.label-con {
    font-size: 14px;
    color: #969799;
    min-width: 5rem;

    .label-con__span {
        font-size: 13px;
        color: #999;
    }
}

.tip-con {
    font-size: 12px;
    color: #fff;
    background: rgba(0, 0, 0, 0.6);
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 2px;
    width: 60px;
    text-align: center;
}
</style>
